import * as fgui from "fairygui-cc";
import EventMgr from "../../../../Core/Event/EventMgr";
import { tween, Vec2 } from "cc";
import UICore from "../../../../Core/Res/UICore";
import { ModelGame } from "../../model/ModelGame";
import { MathUtils } from "../../../../Core/Utils/MathUtils";
import { DateUtils } from "../../../../Core/Utils/DateUtils";
import { time } from "console";
import TimerMgr from "../../../../Core/Timer/TimerMgr";
import { ViewManager } from "../../base/ViewManager";
import { RandomUtils } from "../../../../Core/Utils/RandomUtils";
export class Grid extends fgui.GComponent {
    private ui_icon: fgui.GLoader;
    private text_pos: fgui.GTextField;
    private _active: boolean = true;
    private _gridPos: Vec2;
    private _score: number = 1;//基础分
    private _icon: string = "";
    private _count: number = 0;
    private ctrl_state: fgui.Controller;
    protected onConstruct(): void {
        this.ui_icon = this.getChild("ui_icon") as fgui.GLoader;
        this.text_pos = this.getChild("text_pos") as fgui.GTextField;
        this.ctrl_state = this.getController("ctrl_state");
        this.onClick(this.clickSelf, this);
    }
    public setData(i: number, j: number, isis: boolean = true): void {
        this._gridPos = new Vec2(i, j);
        this.name = `grid_${i}_${j}`;
        this._count = RandomUtils.ins.randomArray([10, 20, 30]);
        this.text_pos.text = DateUtils.ins.getFormatBySecond(this._count);
        if (isis && Math.random() > 0.95) {
            TimerMgr.ins.add(1000, -1, this.countDown, this);
            if (Math.random() > 0.5) {
                this.ctrl_state.selectedIndex = 1;
            } else {
                this.ctrl_state.selectedIndex = 2;
            }
        } else {
            this.ctrl_state.selectedIndex = 0;
        }
    }
    public changePos(i: number, j: number): void {
        this._gridPos = new Vec2(i, j);
        this.name = `grid_${i}_${j}`;
    }
    private countDown(): void {
        this._count -= 1;
        if (this._count <= 0) {
            TimerMgr.ins.remove(this.countDown, this);
            if (this.state == 2) {
                if (ModelGame.getlevel(this._icon) > 1) {
                    ViewManager.ins.showtips("等级减一");
                    ModelGame.setlevel(this._icon, ModelGame.getlevel(this._icon) - 1);
                }
            }
            this.ctrl_state.selectedIndex = 0;
            return;
        }
        this.text_pos.text = DateUtils.ins.getFormatBySecond(this._count);
    }
    public reset(): void {
        TimerMgr.ins.remove(this.countDown, this);
        this.ctrl_state.selectedIndex = 0;
    }
    public get state(): number {
        return this.ctrl_state.selectedIndex;
    }
    public get score(): number {
        return this._score * (ModelGame.getlevel(this._icon) ? ModelGame.getlevel(this._icon) : 1);
    }
    public set u_icon(url: string) {
        this._icon = url;
        this.ui_icon.url = UICore.getURL(`Main`, this._icon);;
    }
    public get u_icon(): string {
        return this._icon;
    }
    public set active(value: boolean) {
        this.visible = value;
    }
    public get active(): boolean {
        return this._active;
    }
    private clickSelf(): void {
        EventMgr.emit("GridClick", this);
    }
    public get gridPos(): Vec2 {
        return this._gridPos;
    }
    public tweenToPos(x: number, y: number, call: Function = null, duration: number = 1): void {
        tween(this as Grid).to(duration, { x: x, y: y }).call(() => {
            if (call) {
                call();
                call = null;
            }
        }, this).start();
    }
    public tweenToScale(scale: number, call: Function, duration: number = 0.3): void {
        tween(this as Grid).to(duration, { scaleX: scale, scaleY: scale }).call(() => {
            if (call) {
                call();
                call = null;
            }
        }, this).start();
    }
    public tweenToPos2(x, y): void {
        tween(this as Grid).to(0.3, { x: x, y: y }).delay(0.2).call(() => {
            this.u_icon = ModelGame.pieceValue;
        }, this).to(0.3, { x: this.x, y: this.y }).start();
    }
}